using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace NBear.Common
{
    public sealed class Util
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Util"/> class.
        /// </summary>
        private Util()
        {
        }

        /// <summary>
        /// Defaults the value.
        /// </summary>
        /// <returns></returns>
        public static object DefaultValue<MemberType>()
        {
            return default(MemberType);
        }

        public static object DefaultValue(Type type)
        {
            return typeof(Util).GetMethod("DefaultValue", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public, null, Type.EmptyTypes, null).MakeGenericMethod(type).Invoke(null, null);
        }

        public static PropertyInfo[] DeepGetProperties(params Type[] types)
        {
            if (types == null || types.Length == 0)
            {
                return new PropertyInfo[0];
            }
            List<PropertyInfo> list = new List<PropertyInfo>();
            foreach (Type t in types)
            {
                if (t != null)
                {
                    foreach (PropertyInfo pi in t.GetProperties())
                    {
                        list.Add(pi);
                    }

                    Type[] interfaceTypes = t.GetInterfaces();

                    if (interfaceTypes != null)
                    {
                        foreach (PropertyInfo pi in DeepGetProperties(interfaceTypes))
                        {
                            bool isContained = false;

                            foreach (PropertyInfo item in list)
                            {
                                if (item.Name == pi.Name)
                                {
                                    isContained = true;
                                    break;
                                }
                            }

                            if (!isContained)
                            {
                                list.Add(pi);
                            }
                        }
                    }
                }
            }

            return list.ToArray();
        }

        public static Type GetOriginalTypeOfArrayType(Type returnType)
        {
            return GetType(returnType.FullName.TrimEnd('[', ']'));
        }

        public static Type GetType(string fullName)
        {
            Type t = null;

            try
            {
                t = Type.GetType(fullName);
            }
            catch
            {
            }

            if (t == null)
            {
                try
                {
                    t = BaseEntityFactory.GetEntityType(fullName);
                }
                catch
                {
                }
            }

            if (t == null)
            {
                try
                {
                    Assembly[] asses = AppDomain.CurrentDomain.GetAssemblies();

                    foreach (Assembly ass in asses)
                    {
                        try
                        {
                            t = ass.GetType(fullName);
                        }
                        catch
                        {
                        }

                        if (t != null)
                        {
                            break;
                        }
                    }
                }
                catch
                {
                }
            }

            return t;
        }
    }
}
